package com.namecard.namecardapi.util;

import java.util.UUID;

/**
 * @author wzx
 */
public class StringUtil {

    /**
     采用UUID获取唯一的CardId
     */
    public static String generateShortId() {
        UUID uuid = UUID.randomUUID();
        long mostSigBits = uuid.getMostSignificantBits();
        long leastSigBits = uuid.getLeastSignificantBits();

        // 将 UUID 的最高位和最低位转换成 16 进制字符串
        String mostSigHex = Long.toHexString(mostSigBits);
        String leastSigHex = Long.toHexString(leastSigBits);

        // 拼接最高位和最低位，并截取前11位作为最终的短 ID
        String shortId = mostSigHex + leastSigHex;
        return shortId.substring(0, 11);
    }

    /**
     * 获取文件的后缀
     */
    public static String getFileSuffix(String fileName) {
        Integer index = fileName.lastIndexOf(".");
        if (index == -1) {
            return "";
        }
        String suffix = fileName.substring(index);
        return suffix;
    }
}
